package class_wuziqi;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.net.Socket;

import javax.swing.BorderFactory;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.plaf.nimbus.NimbusLookAndFeel;
import javax.swing.table.DefaultTableModel;

public class MainPro extends JFrame implements ActionListener{
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private static final int WIDTH=990;
	private static final int HEIGHT=700;
	private JTextArea user_info;
	private DefaultTableModel dtm;
	private JTable jtable;
	private JTextArea jta;
	private JTextField jtf;
	private JButton send;
	private Socket socket;
	private JPanel lp;
	private MainPro(){
		Jiemian();
	}
	private void Jiemian(){
		setTitle("五子棋_局域网");
		setSize(WIDTH, HEIGHT);
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
		Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
		setLocation((screenSize.width-WIDTH)/2, (screenSize.height-HEIGHT)/2);
		JPanel right=new JPanel();
		right.setLayout(new BoxLayout(right, BoxLayout.PAGE_AXIS));
		right.setBorder(BorderFactory.createLineBorder(Color.DARK_GRAY));
		add(right, BorderLayout.EAST);
		//用户头像与信息
		JPanel right_top=new JPanel();
		right_top.setLayout(new BorderLayout());
		JLabel tx=new JLabel();
		tx.setIcon(new ImageIcon(getClass().getResource("/pic/infoPanelLeft.png")));
		right_top.add(tx, BorderLayout.WEST);
		user_info=new JTextArea(3,10);
		right_top.add(user_info, BorderLayout.CENTER);
		right.add(right_top);
		//联机信息显示
		JPanel clientjp=new JPanel();
		clientjp.setLayout(new BorderLayout());
		right.add(clientjp);
		dtm =new DefaultTableModel(new Object[][]{}, new String[]{"昵称","IP","联机时间"});
		jtable=new JTable();
		jtable.setModel(dtm);
		jtable.setAutoResizeMode(JTable.AUTO_RESIZE_OFF);
		clientjp.add(jtable);
		JScrollPane csp=new JScrollPane();
		csp.setPreferredSize(new Dimension(223, 140));
		csp.setViewportView(jtable);
		clientjp.add(csp, BorderLayout.CENTER);
		//聊天窗口
		JPanel chatjp=new JPanel();
		chatjp.setLayout(new BorderLayout());
		right.add(chatjp);
		jta=new JTextArea();
		jta.setEditable(false);
		jta.setPreferredSize(new Dimension(100,250));
		chatjp.add(jta, BorderLayout.CENTER);
		JPanel down=new JPanel();
		down.setLayout(new BorderLayout());
		jtf=new JTextField();
		down.add(jtf, BorderLayout.CENTER);
		send=new JButton("发送");
		send.addActionListener(this);
		down.add(send, BorderLayout.EAST);
		chatjp.add(down, BorderLayout.SOUTH);		
		//聊天窗
		try {
			socket =new Socket("localhost", 2727);
			cReceive(socket, jta);
		} catch (Exception e) {
			e.printStackTrace();
		}
		//左窗口部分
		lp=new Left_Panel();
		add(lp, BorderLayout.CENTER);
	}
	
	private void cReceive(Socket socket,JTextArea jta) {
		Client_Receive cr=new Client_Receive(socket, jta);
		Thread t=new Thread(cr);
		t.start();
	}
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			
			@Override
			public void run() {
				try {
					UIManager.setLookAndFeel(new NimbusLookAndFeel());//定义风格
					new MainPro();
				} catch (UnsupportedLookAndFeelException e) {
					e.printStackTrace();
				}
			}
		});
	}
	@Override
	public void paint(Graphics g) {
		super.paint(g);
		System.out.println(getWidth()+"   "+getHeight());
	}
	@Override
	public void actionPerformed(ActionEvent e) {
		try {
			ObjectOutputStream oos=new ObjectOutputStream(socket.getOutputStream());
			String message=jtf.getText();
			oos.writeObject(message);
			oos.flush();
			jtf.setText("");
		} catch (Exception e1) {
			e1.printStackTrace();
		}
		
	}
}
